package com.ygl.core.configuration;

import com.ygl.common.constant.DiscoveryConstant;
import com.ygl.core.adapter.DefaultEnvironmentRouteAdapter;
import com.ygl.core.adapter.EnvironmentRouteAdapter;
import com.ygl.core.cache.RuleCache;
import com.ygl.core.context.CoreContextAware;
import com.ygl.core.listener.loadbalance.EnvironmentFilterLoadBalanceListener;
import com.ygl.core.listener.loadbalance.RegionFilterLoadBalanceListener;
import com.ygl.core.listener.loadbalance.VersionFilterLoadBalanceListener;
import com.ygl.core.listener.register.HostFilterRegisterListener;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * author: yanggld
 * date: 2019/12/14 12:00
 * version: 1.0
 * description: 项目启动时配置，核心配置初始化Bean
 */
@Configuration
public class CoreAutoConfiguration {

    @Bean
    public HostFilterRegisterListener hostFilterRegisterListener(){
        return new HostFilterRegisterListener();
    }

    @Bean
    public VersionFilterLoadBalanceListener versionFilterLoadBalanceListener() {
        return new VersionFilterLoadBalanceListener();
    }

    @Bean
    public RegionFilterLoadBalanceListener regionFilterLoadBalanceListener() {
        return new RegionFilterLoadBalanceListener();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(value = DiscoveryConstant.SPRING_APPLICATION_ENVIRONMENT_ISOLATION_ENABLED, matchIfMissing = false)
    public EnvironmentFilterLoadBalanceListener environmentFilterLoadBalanceListener() {
        return new EnvironmentFilterLoadBalanceListener();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(value = DiscoveryConstant.SPRING_APPLICATION_ENVIRONMENT_ISOLATION_ENABLED, matchIfMissing = false)
    public EnvironmentRouteAdapter environmentRouteAdapter() {
        return new DefaultEnvironmentRouteAdapter();
    }

    @Bean
    public CoreContextAware coreContextAware(){
        return new CoreContextAware();
    }

    @Bean
    public RuleCache ruleCache() {
        return new RuleCache();
    }

}
